<!-- 报告送审 -->
<template>
  <div>
    <card :bordered="false">
      <p slot="title">报告送审</p>
      <table id="reportTria" width='100%' border="1" cellspacing="0" cellpadding="0" ref="print">
        <tr>
          <th colspan="4">报告送审表</th>
        </tr>
        <tr>
          <td width="25%">项目编号</td>
          <td colspan="3" class='sizeStyle'>
            {{info.info.ProjectNumber}}
          </td>
        </tr>
        <tr>
          <td>项目名称</td>
          <td colspan="3" class='sizeStyle'>
            <!-- <Input v-model="info.info.Project_name" :disabled='viewOnly'/> -->
            {{info.info.ProjectName}}
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'projectName')" v-if='projectNameList.length>0'/>
            <Oldname v-if="projectName" :oldName="projectNameList"></Oldname>
          </td>
        </tr>
        <tr>
          <td>建设单位</td>
          <td colspan="3">
            <Input v-model="info.info.BuildUnit" :disabled='viewOnly'/>
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'buildName')" v-if='buildNameList.length>0'/>
            <Oldname v-if="buildName" :oldName="buildNameList"></Oldname>
          </td>
        </tr>
        <tr>
          <td>建设地点</td>
          <td colspan="3">
            <Input v-model="info.info.BuildAddress" :disabled='viewOnly'/>
          </td>
        </tr>
        <tr>
          <td>报告类型</td>
          <td colspan="3">
            <Input v-model="info.info.ReportType" :disabled='viewOnly'/>
          </td>
        </tr>
        <tr>
          <td>项目负责人</td>
          <td width="25%">
            <Input v-model="info.PerjectLeader" :disabled='viewOnly'/>
          </td>
          <td width="25%">项目组其他成员</td>
          <td class='sizeStyle'>
            {{this.cyz.data}}
          </td>
        </tr>
        <tr>
          <td>审批部门</td>
          <td>
            <Input v-model="info.info.ApprovalUnit" :disabled='viewOnly'/>
          </td>
          <td>送审时间</td>
          <td>
            <Input v-model="info.info.AuditDate" :disabled='viewOnly' />
            <!-- {{this.newtime}} -->
          </td>
        </tr>
        <tr>
          <td>送审版报告</td>
          <td colspan="3">
            <Col span="12"><UploadFile v-model="info.info.ReportAuditor" :upName="ReportAuditor" UploadFileName="ReportAuditor" :UploadFileId="$route.query.ProjectId" :isDisabled='viewOnly'/></Col>
            <Col span="12"><DownloadButton v-if="viewOnly" :downData="downloadInfo.ReportAuditor"></DownloadButton></Col>
            <p style="font-size:12px;color:#666">上传文件格式为docx、doc、PDF、图片、压缩文件</p>
          </td>
        </tr>
      </table>
      <Row type="flex" justify="space-between" class="topmargin">
        <Col span="12">
        <PrintButton v-if="flag" Url="print/printAudit" :Project_id="info.info.Id"></PrintButton>
        </Col>
        <Col span="12">
        <div style="float: right">
          <Button type="primary" @click="submit('save')" :disabled='viewOnly'>保存</Button>
          <Button type="primary" @click="submit('submit')" :disabled='viewOnly'>提交评估</Button>
        </div>
        </Col>
      </Row>
      <StepBar :status="5" :viewOnly='viewOnly'/>

    </card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 项目名称、建设单位、法人、联系人、联系电话留痕列表
        projectName: false,
        buildName: false,
        PerjectType: 0,
        projectNameList: [], 
        buildNameList: [],  
        downloadInfo: {},
        viewOnly: false,  // 允许查看
        flag: false,
        info: {
          info: {}
        },
        query: [],
        cyz: '',
        ReportAuditor: "",
        newtime: '',
        updatedInfo: 0,
        setButton: 0,
      }
    },
    mounted() {
      // this.newtime = this.$moment().format('YYYY-MM-DD')
      // console.log(this.newtime)
      // let query = this.$route.query
      // this.query = query
      // this.getProject(this.query.id, this.query.Project_id)

      //判断是否有ID
      if (this.$route.query.ProjectId && this.$route.query.ProjectId != "") {
        this.getProject(this.$route.query.ProjectId);
        // 项目成员组
        this.findStaffs()
        if(this.$route.query.viewOnly==1 || localStorage.getItem('userOrg') ==7) {
          this.viewOnly = true
        }
      } else {
        //跳转首页
        this.$router.push({
          name: "index"
        });
      }     
    },
    methods: {
      // 名字去重显示
      viewClick(e,nameShow) {
        if(!this[nameShow]) {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-up'
        }else {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-down'
        }
        this[nameShow] = !this[nameShow]
      },
      /* 获取当前创建项目 */
      getProject(Id) {
        // axios.get('/projectInformation/findProjectByID', {
        this.$post('ProjectInfo.findProjectByID', {
          // params: {
            Id: Id || this.$route.query.ProjectId
          // }
        }).then(res => {
          console.log(res)
          this.downloadInfo = res.doc
          this.info = res
          this.PerjectType = res.info.PerjectType
          // 项目名字留痕
          if(res.pt2List.length>0) {
            var arrAll = res.pt2List //所有更改记录
            function arrayUnique2(arr, ProjectName) {
              var hash = {};
              return arr.reduce(function (item, next) {
                hash[next[ProjectName]] ? '' : hash[next[ProjectName]] = true && item.push(next);
                return item;
              }, []);
            }
            var projectNameList = arrayUnique2(arrAll, "ProjectName")
            if(projectNameList[0].ProjectName==this.info.info.ProjectName) {
              projectNameList = arrayUnique2(arrAll, "ProjectName").slice(1)
            }
            projectNameList.forEach(e => {
              this.projectNameList.push({name: e.ProjectName,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });

            var buildNameList = arrayUnique2(arrAll, "BuildUnit")
            if(buildNameList[0].BuildUnit==this.info.info.BuildUnit) {
              buildNameList = arrayUnique2(arrAll, "BuildUnit").slice(1)
            }
            buildNameList.forEach(e => {
              this.buildNameList.push({name: e.BuildUnit,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });          
          }
          
          localStorage.projectType = this.info.info.PerjectType
          this.flag = true
          this.ReportAuditor = this.upWord(res.doc.ReportAuditor)
        }).catch(err => {
          console.log(err)
          this.$Message.error('获取当前项目失败!')
          // alert('请求崩溃1231')
        })
      },
      findStaffs() {
        axios.get('/audit/findStaffs2', {
          params: {
            ProjectId: this.$route.query.ProjectId
          }
        }).then(res => {
          // console.log(res)
          this.cyz = res
        })
      },
      // 保存 save / 提交 submit
      submit(type) {
        let info = this.info.info
        info.BtnWay = type == 'save' ? 1 : 2
        info.PerjectType = type == 'save' ? 11 : 11

        if(type == 'save'){
          if(this.setButton == 0){
            this.setButton = 1
            setTimeout(()=>{this.setButton = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                //   if(this.info.info.Perject_type == 11){
                //   axios.get('member/findByMembers').then((res)=>{
                //     var members = res.members
                //     // members = members.split(',')
                //     console.log(members)
                //     social({
                //       method: 'post',
                //       url: '/info/notice',
                //       params: {
                //         content: this.info.Project_name + '项目已提交评估，请查阅报告送审情况，并关注项目技术评估事宜！',
                //         app_name: '环评管控系统',
                //         app: 'eao',
                //         type: members,
                //         uid: -1
                //       },
                //     }).then((res)=>{})
                //   })
                // }
                  this.$router.push({
                    name: "technologyAssessment",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {this.$Message.error('请求失败!')})
          }else{
            this.$Message.warning('连续点击保存时间过短，最少间隔1分钟!')
          }
        }else{
          if(this.updatedInfo == 0){
            this.updatedInfo = 1
            setTimeout(()=>{this.updatedInfo = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                  console.log(this.PerjectType)
                  if(this.PerjectType == 11){
                  axios.get('member/findByMembers').then((res)=>{
                    var members = res.members
                    if(!(members==null||members=="")) {
                    // members = members.split(',')
                      console.log(this.info.info.ProjectName)
                      social({
                        method: 'post',
                        url: '/info/notice',
                        params: {
                          content: this.info.info.ProjectName + '项目已提交评估，请查阅报告送审情况，并关注项目技术评估事宜！',
                          app_name: '环评管控系统',
                          app: 'eao',
                          type: members,
                          uid: -1
                        },
                      }).then((res)=>{})
                    }
                  })
                }
                  this.$router.push({
                    name: "technologyAssessment",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {this.$Message.error('请求失败!')})
          }else{
            this.$Message.warning('连续点击提交时间过短，最少间隔1分钟!')
          }
        }
        // if(this.setButton == 0){
        //   this.setButton = 1
        //   setTimeout(()=>{this.setButton = 0},60000)
        // let info = this.info.info
        // info.Audit_date = this.newtime
        // info.btn_way = type == 'save' ? 1 : 2
        // info.Perject_type = type == 'save' ? 11 : 11
        // console.log(info)
        // // if(this.updatedInfo <= 3){
        // //   this.$Message.warning('未修改数据无法提交')
        // //   return
        // // }
        // axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
        //   if (res._STATUS == 1) {
        //     let title = res._MESSAGE
        //     if (type == 'save') {
        //       this.$Modal.success({
        //         title,
        //       })
        //     } else {
        //       if(this.info.info.Perject_type == 11){
        //       axios.get('member/findByMembers').then((res)=>{
        //         var members = res.members
        //         // members = members.split(',')
        //         console.log(members)
        //         social({
        //           method: 'post',
        //           url: '/info/notice',
        //           data: {
        //             content: this.info.Project_name + '项目已提交评估，请查阅报告送审情况，并关注项目技术评估事宜！',
        //             app_name: '环评管控系统',
        //             app: 'eao',
        //             type: members,
        //             uid: -1
        //           },
        //         }).then((res)=>{})
        //       })
        //     }
        //       this.$router.push({
        //         name: "technologyAssessment",
        //         query: {
        //           Project_id: this.$route.query.Project_id
        //         }
        //       })
        //     }
        //   } else {
        //     // 提示
        //     this.$Modal.error({
        //       title:res._MESSAGE
        //     })
        //   }
        // }).catch(err => {
        //   console.log(err)
        //   this.$Message.error('请求失败!')
        // })
        // }else{
        //   this.$Message.warning('请勿重复提交!')
        // }
      },
      upWord(url){        //分割字符串
        if(url == null){
          return '上传'
        }else{
          var upWord = url;
          upWord = upWord.split("/");
          upWord = upWord.slice(-1);
          upWord = upWord.join('');
          // console.log(upWord)
          return upWord
        }
      },
    },
  }
</script>

<style lang="less">
  #reportTria {
    td.sizeStyle {
      font-size:12px;
    }
    textarea.ivu-input {
      font-size: 12px;
    }
    .ivu-input {
      border: none !important;
      text-align: center;
      height: 30px;

      input {
        outline: medium;
      }
    }
    .ivu-input[disabled], fieldset[disabled] .ivu-input {
      background-color: #fff; 
      color: #333;
      font-size: 12px;
    }
  }
</style>